M4
m4 is a general-purpose macro processor included in all UNIX-like operating systems, and is a component of the POSIX standard. The language was designed by Brian Kernighan and Dennis Ritchie for the original versions of UNIX. It is an extension of an earlier macro processor m3, written by Ritchie for the AP-3 minicomputer.Brian W. Kernighan and Dennis M. Ritchie. The m4 macro processor. Technical report, Bell Laboratories, Murray Hill, New Jersey, USA, 1977. pdf The macro preprocessor operates as a text-replacement tool. It is employed to re-use text templates, typically in computer programming applications, but also in text editing and text-processing applications. Most users require m4 as a dependency of GNU autoconf. History Macro processors became popular when programmers commonly used assembly language. In those early days of programming, programmers noted that much of their programs consisted of repeated text, and they invented simple means for reusing this text. Programmers soon discovered the advantages not only of reusing entire blocks of text, but also of substituting different values for similar parameters. This defined the usage range of macro processors. Kernighan and Ritchie developed m4 in 1977, basing it on the ideas of Christopher Strachey. The distinguishing features of this style of macro preprocessing included: * free-form syntax (not line-based like a typical macro preprocessor designed for assembly-language processing) * the high degree of re-expansion (a macro's arguments get expanded twice: once during scanning and once at interpretation time) The implementation of Rational Fortran used m4 as its macro engine from the beginning; and most Unix variants ship with it. many applications continue to use m4 as part of the GNU Project's autoconf. It also appears in the configuration process of sendmail (a widespread mail transfer agent) and for generating footprints in the gEDA toolsuite. The SELinux Reference Policy relies heavily on the m4 macro processor. m4 has many uses in code generation, but (as with any macro processor) problems can be hard to debug.Kenneth J. Turner. Exploiting the m4 macro language. Technical Report CSM-126, Department of Computing Science and Mathematics, University of Stirling, Scotland, September 1994. pdf Features m4 offers these facilities: * a free-form syntax, rather than line based syntax * a high degree of macro expansion (arguments get expanded during scan and again during interpretation) * text replacement * parameter substitution * file inclusion * string manipulation * conditional evaluation * arithmetic expressions * system interface * programmer diagnostics * programming language independent * human language independent * provides programming language capabilities Unlike most earlier macro processors, m4 does not target any particular computer or human language; historically, however, its development originated for supporting the Ratfor dialect of Fortran. Unlike some other macro processors, m4 is Turing-complete as well as a practical programming language. Unquoted identifiers which match defined macros are replaced with their definitions. Placing identifiers in quotes suppresses expansion until possibly later, such as when a quoted string is expanded as part of macro replacement. Unlike most languages, strings in m4 are quoted using the backtick (`) as the starting delimiter, and apostrophe (') as the ending delimiter. Separate starting and ending delimiters allows the arbitrary nesting of quotation marks in strings to be used, allowing a fine degree of control of how and when macro expansion takes place in different parts of a string. Iksampol The following fragment gives a simple example that could form part of a library for generating HTML code. It defines a commented macro to number sections automatically: divert(-1) m4 has multiple output queues that can be manipulated with the `divert' macro. Valid queues range from 0 to 10, inclusive, with the default queue being 0. As an extension, GNU m4 supports more diversions, limited only by integer type size. Calling the `divert' macro with an invalid queue causes text to be discarded until another call. Note that even while output is being discarded, quotes around `divert' and other macros are needed to prevent expansion. # Macros aren't expanded within comments, meaning that keywords such # as divert and other built-ins may be used without consequence. # HTML utility macro: define(`H2_COUNT', 0) # The H2_COUNT macro is redefined every time the H2 macro is used: define(`H2', `define(`H2_COUNT', incr(H2_COUNT)) H2_COUNT. $1 ') divert(1)dnl dnl dnl The dnl macro causes m4 to discard the rest of the line, thus dnl preventing unwanted blank lines from appearing in the output. dnl H2(First Section) H2(Second Section) H2(Conclusion) dnl divert(0)dnl dnl undivert(1)dnl One of the queues is being pushed to output. Processing this code with m4 generates the following text: 1. First Section 2. Second Section 3. Conclusion Implementations FreeBSD, NetBSD, and OpenBSD provide independent implementations of the m4 language. Furthermore, the Heirloom Project Development Tools includes a free version of the m4 language, derived from OpenSolaris. GNU m4 is an implementation of m4 for the GNU Project.GNU m4 web site "GNU M4", accessed May 28, 2016.GNU m4 manual, online and for download in HTML, PDF, and other forms. "GNU M4 — GNU macro processor", accessed May 28, 2016. It is designed to avoid many kinds of arbitrary limits found in traditional m4 implementations, such as maximum line lengths, maximum size of a macro and number of macros. Removing such arbitrary limits is one of the stated goals of the GNU Project."GNU Coding Standards: Writing Robust Programs". quote: "Avoid arbitrary limits on the length or number of any data structure". The GNU Autoconf package makes extensive use of the features of GNU m4. GNU m4 is currently maintained by Gary V. Vaughan and Eric Blake. Released under the terms of the GNU General Public License, GNU m4 is free software. Päkeijs macports ---> Fetching archive for m4 ---> Attempting to fetch m4-1.4.17_0.darwin_13.x86_64.tbz2 from http://jog.id.packages.macports.org/macports/packages/m4 ---> Attempting to fetch m4-1.4.17_0.darwin_13.x86_64.tbz2.rmd160 from http://jog.id.packages.macports.org/macports/packages/m4 ---> Installing m4 @1.4.17_0 ---> Activating m4 @1.4.17_0 ---> Cleaning m4 Rībiuldiŋ git clone tom@BKGOVWKS04:~/Desktop/Development$ git clone git://git.sv.gnu.org/m4.git Cloning into 'm4'... remote: Counting objects: 18074, done. remote: Compressing objects: 100% (3160/3160), done. remote: Total 18074 (delta 14690), reused 18074 (delta 14690) Receiving objects: 100% (18074/18074), 6.85 MiB | 628.00 KiB/s, done. Resolving deltas: 100% (14690/14690), done. Checking connectivity... done. tom@BKGOVWKS04:~/Desktop/Development$ cd m4 tom@BKGOVWKS04:~/Desktop/Development/m4$ ls -la 總計 860 drwxrwxr-x 10 tom tom 4096 3月 20 12:47 . drwxrwxr-x 34 tom tom 4096 3月 20 12:47 .. -rw-rw-r-- 1 tom tom 2454 3月 20 12:47 AUTHORS -rw-rw-r-- 1 tom tom 147 3月 20 12:47 .autom4te.cfg -rwxrwxr-x 1 tom tom 148310 3月 20 12:47 bootstrap -rw-rw-r-- 1 tom tom 4123 3月 20 12:47 bootstrap.conf drwxrwxr-x 5 tom tom 4096 3月 20 12:47 build-aux -rw-rw-r-- 1 tom tom 2514 3月 20 12:47 cfg.mk -rw-rw-r-- 1 tom tom 476602 3月 20 12:47 ChangeLog.old -rw-rw-r-- 1 tom tom 10380 3月 20 12:47 configure.ac -rw-rw-r-- 1 tom tom 35158 3月 20 12:47 COPYING drwxrwxr-x 3 tom tom 4096 3月 20 12:47 doc drwxrwxr-x 8 tom tom 4096 3月 20 12:47 .git -rw-rw-r-- 1 tom tom 214 3月 20 12:47 .gitattributes -rw-rw-r-- 1 tom tom 1030 3月 20 12:47 .gitignore -rw-rw-r-- 1 tom tom 99 3月 20 12:47 .gitmodules -rw-rw-r-- 1 tom tom 15622 3月 20 12:47 HACKING drwxrwxr-x 2 tom tom 4096 3月 20 12:47 m4 -rw-rw-r-- 1 tom tom 14259 3月 20 12:47 Makefile.am drwxrwxr-x 2 tom tom 4096 3月 20 12:47 modules -rw-rw-r-- 1 tom tom 53816 3月 20 12:47 NEWS -rw-rw-r-- 1 tom tom 6558 3月 20 12:47 NO-THANKS drwxrwxr-x 2 tom tom 4096 3月 20 12:47 po -rw-rw-r-- 1 tom tom 7 3月 20 12:47 .prev-version -rw-rw-r-- 1 tom tom 5204 3月 20 12:47 README -rw-rw-r-- 1 tom tom 1090 3月 20 12:47 README-alpha drwxrwxr-x 2 tom tom 4096 3月 20 12:47 src drwxrwxr-x 2 tom tom 4096 3月 20 12:47 tests -rw-rw-r-- 1 tom tom 5711 3月 20 12:47 TODO -rw-rw-r-- 1 tom tom 24 3月 20 12:47 .x-update-copyright Dipendènçi tom@BKGOVWKS04:~/Desktop/Development/m4$ ./bootstrap bootstrap: error: Prerequisite 'help2man' not found. Please install it, or bootstrap: 'export HELP2MAN=/path/to/help2man'. bootstrap: error: Prerequisite 'makeinfo' not found. Please install it, or bootstrap: 'export MAKEINFO=/path/to/makeinfo'. bootstrap: error: HACKING explains how to obtain these prerequisite programs: bootstrap: Program Min_version Homepage bootstrap: ----------------------------------------------------------- bootstrap: help2man 1.29 http://www.gnu.org/s/help2man bootstrap: makeinfo 4.13 http://www.gnu.org/s/texinfo bootstrap: xz - http://tukaani.org/xz bootstrap: autoconf 2.62 http://www.gnu.org/s/autoconf bootstrap: automake 1.10b http://www.gnu.org/s/automake bootstrap: libtoolize 2.2 http://www.gnu.org/s/libtool bootstrap: autopoint 0.16 http://www.gnu.org/s/gettext bootstrap: ----------------------------------------------------------- Instol dipendènçi tom@BKGOVWKS04:~/Desktop/Development/m4$ sudo apt-get install help2man texinfo autoconf automake libtool autopoint 正在讀取套件清單... 完成 正在重建相依關係 正在讀取狀態資料... 完成 autoconf 已是最新版本。 autoconf 被設定為手動安裝。 automake 已是最新版本。 automake 被設定為手動安裝。 libtool 已是最新版本。 libtool 被設定為手動安裝。 autopoint 已是最新版本。 autopoint 被設定為手動安裝。 下列的額外套件將被安裝： * libtext-unidecode-perl * libtool ** libxml-libxml-perl ** libxml-namespacesupport-perl ** libxml-sax-base-perl ** libxml-sax-expat-perl ** libxml-sax-perl 建議套件： * texlive-base * texlive-latex-base * texlive-generic-recommended * texinfo-doc-nonfree 下列【新】套件將會被安裝： # help2man # libtext-unidecode-perl # libxml-libxml-perl # libxml-namespacesupport-perl # libxml-sax-base-perl # libxml-sax-expat-perl # libxml-sax-perl texinfo 升級 0 個，新安裝 8 個，移除 0 個，有 0 個未被升級。 需要下載 1,183 kB 的套件檔。 此操作完成之後，會多佔用 5,467 kB 的磁碟空間。 Do you want to continue? Y/n y Dawnloudiŋ dipendènçis #下載:1 http://hk.archive.ubuntu.com/ubuntu/ trusty/main help2man amd64 1.44.1 kB #下載:2 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libtext-unidecode-perl all 0.04-2 kB #下載:3 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libxml-namespacesupport-perl all 1.11-1 kB #下載:4 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libxml-sax-base-perl all 1.07-1 kB #下載:5 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libxml-sax-perl all 0.99+dfsg-2ubuntu1 kB #下載:6 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libxml-libxml-perl amd64 2.0108+dfsg-1 kB #下載:7 http://hk.archive.ubuntu.com/ubuntu/ trusty/main libxml-sax-expat-perl all 0.40-2 kB #下載:8 http://hk.archive.ubuntu.com/ubuntu/ trusty/main texinfo amd64 5.2.0.dfsg.1-2 kB 取得 1,183 kB 用了 7秒 (153 kB/s) Instoling dipendènçis （讀取資料庫 ... 目前共安裝了 332487 個檔案和目錄。） 選取了原先未選的套件 help2man。 準備解開 .../help2man_1.44.1_amd64.deb ... 解開 help2man (1.44.1) 中... 選取了原先未選的套件 libtext-unidecode-perl。 準備解開 .../libtext-unidecode-perl_0.04-2_all.deb ... 解開 libtext-unidecode-perl (0.04-2) 中... 選取了原先未選的套件 libxml-namespacesupport-perl。 準備解開 .../libxml-namespacesupport-perl_1.11-1_all.deb ... 解開 libxml-namespacesupport-perl (1.11-1) 中... 選取了原先未選的套件 libxml-sax-base-perl。 準備解開 .../libxml-sax-base-perl_1.07-1_all.deb ... 解開 libxml-sax-base-perl (1.07-1) 中... 選取了原先未選的套件 libxml-sax-perl。 準備解開 .../libxml-sax-perl_0.99+dfsg-2ubuntu1_all.deb ... 解開 libxml-sax-perl (0.99+dfsg-2ubuntu1) 中... 選取了原先未選的套件 libxml-libxml-perl。 準備解開 .../libxml-libxml-perl_2.0108+dfsg-1_amd64.deb ... 解開 libxml-libxml-perl (2.0108+dfsg-1) 中... 選取了原先未選的套件 libxml-sax-expat-perl。 準備解開 .../libxml-sax-expat-perl_0.40-2_all.deb ... 解開 libxml-sax-expat-perl (0.40-2) 中... 選取了原先未選的套件 texinfo。 準備解開 .../texinfo_5.2.0.dfsg.1-2_amd64.deb ... 解開 texinfo (5.2.0.dfsg.1-2) 中... Processing triggers for install-info (5.2.0.dfsg.1-2) ... Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Setiŋ 設定 help2man (1.44.1) ... 設定 libtext-unidecode-perl (0.04-2) ... 設定 libxml-namespacesupport-perl (1.11-1) ... 設定 libxml-sax-base-perl (1.07-1) ... 設定 libxml-sax-perl (0.99+dfsg-2ubuntu1) ... update-perl-sax-parsers: Registering Perl SAX parser XML::SAX::PurePerl with priority 10... update-perl-sax-parsers: Updating overall Perl SAX parser modules info file... Creating config file /etc/perl/XML/SAX/ParserDetails.ini with new version 設定 libxml-libxml-perl (2.0108+dfsg-1) ... update-perl-sax-parsers: Registering Perl SAX parser XML::LibXML::SAX::Parser with priority 50... update-perl-sax-parsers: Registering Perl SAX parser XML::LibXML::SAX with priority 50... update-perl-sax-parsers: Updating overall Perl SAX parser modules info file... Replacing config file /etc/perl/XML/SAX/ParserDetails.ini with new version 設定 libxml-sax-expat-perl (0.40-2) ... update-perl-sax-parsers: Registering Perl SAX parser XML::SAX::Expat with priority 50... update-perl-sax-parsers: Updating overall Perl SAX parser modules info file... Replacing config file /etc/perl/XML/SAX/ParserDetails.ini with new version 設定 texinfo (5.2.0.dfsg.1-2) ... Rīkwairen bai * gettext See also * C preprocessor * Macro (computer science) * Make * Template processor * Web template system References Ikstörnol liŋks * * m4 @ gnu* GNU m4 website * GNU m4 manual * m4 tutorial * Macro Magic: m4, Part One and Part Two Category:Macro programming languages Category:Unix programming tools Category:Unix SUS2008 utilities